common={
	initCatalogueMagePage:function(){
		this.Authorization= window.localStorage.getItem("auth_token") || '';
		// 绑定dom对象
		this.obj = {
			searchText:$('#searchText'),
		};
		// 声明全局变量
		this.vars = {
			map:"",
			map1:"",
			datatable:[],
			dataCreat:{
				name:"",
				lng:"",
				lat:"",
				riskRadius:"",
				riskText:"",
				warningRadius:"",
				warningText:"",
				vigilanceRadius:"",
				vigilanceText:"",
				emergencyDesc:"",
				tranType:"",
			},
			searchTable:{
				name:"",
				status:"",
				page:1,
				size:10
			},
			searchTabmove:{
				emergencyName:"",
				startTime:"",
				endTime:"",
				page:1,
				size:10
			},
		};
		this.selectedValue();
		this.time();
		this.map();
		this.ajaxtable();
		this.ajaxbaogao()
		this.btnEvent();

	},
	time:function(){
		var self=this;
		$('.J-datepickerTime-range').datePicker({
			format: 'YYYY-MM-DD HH:mm',
			isRange: true,
			hide: function () {
				self.vars.searchTabmove.startTime=this.$input.eq(0).val();
				self.vars.searchTabmove.endTime=this.$input.eq(1).val();
			}
		});
	},
	tableEvent:function(id,data,search,columns,paginationShow,total){
		var self=this;
		$('#' + id).bootstrapTable({
			method: 'get',
			cache: false,
			height: 430,
			pagination: paginationShow,
			pageSize: search.size,
			pageNumber:search.page,
			showColumns: true,
			striped: true,
			columns: columns,
			data :data,
			sidePagination: 'server',
			totalRows: total,
			onPageChange: function (size, number) {
				console.log(size)
				console.log(number)
				search.size=size;
				search.page=number;
				self.ajaxtable();
				self.ajaxbaogao();
			},
			formatNoMatches: function(){
				return '无符合条件的记录';
			}
		});
	},
	creatTable:function(res){
		var self=this;
		var columnsOne = [
			{
				field: 'id',
				title: '序号',
				align: 'center',
				formatter: function (value, row, index) {
					var pageSize=self.vars.searchTable.size;
					var pageNumber=self.vars.searchTable.page;
					return pageSize * (pageNumber - 1) + index + 1;
				}
			},
			{
				field: 'name',
				title: '紧急事件名称',
			},
			{
				field: 'createMan',
				title: '事件发起人',
			},
			{
				field: 'createManPhone',
				title: '事件发起人电话',
			},
			{
				field: '',
				title: '行业类型',
			},
			{
				field: 'emergencyDesc',
				title: '描述',
			},
			{
				field: 'status',
				title: '事件状态',
				formatter: function (value, row, index) {
					if(value=="0"){
						return "待开启"
					}else if(value=="1"){
						return "进行中"
					}else{
						return "已关闭"
					}
				}

			},
			{
				field: 'startTime',
				title: '启动时间',
			},
			{
				field: 'endTime',
				title: '结束时间',
			},
			{
				field: 'caozuo',
				title: '操作',
				formatter: self.operateFormatterManage,
				events: window.operateEvents = {
					'click .ran_xq-manage': function (e, value, row, index) {
						$("#gzDialogbj").show();
						self.update(row)
					},
					'click .dele-row-manage': function (e, value, row, index) {
						self.delate(row);
					}
				}
			}
		];
		self.tableEvent("check_detail",res.data,self.vars.searchTable,columnsOne,true,res.total);
	},
	ajaxtable:function(){
		var self=this;
		self.vars.searchTable.owersName=$(".rtable_input").find("input").val();
		jrequest.get(urls.datajjsj,self.vars.searchTable).then(res => {
			console.log("123",res)
			self.vars.datatable = res.data;
			$('#check_detail').bootstrapTable('destroy');
			self.creatTable(res)
		})
	},
	update:function(row){
		var self=this;
		$("#gzDialogbj").show();
		$("#gzDialog").hide();
		console.log(row)
		var xqurlbj=HTTP+urls.datajjsj;
		var dataCount=$.map(self.vars.dataCreat,function(value,key){
			return key
		});
		$.each(dataCount,function(i,dom){
			if(dom=="name"){
				$("#gzDialogbj .tantk_right .tantk_Input").eq(0).find("input").val(row[dom])
			}else if(dom=="lat"|| dom=="lng"){
				$("#gzDialogbj .tantk_right .tantk_Input").eq(1).find("input").val(row.lng+","+row.lat);
			}else{
				$("#gzDialogbj .tantk_right .tantk_Input").eq(i-1).find("input").val(row[dom])
			}
		});
		var position=[],mapradius=[];
		position.push(row.lng,row.lat);
		mapradius.push(row.riskRadius,row.warningRadius,row.vigilanceRadius);
		self.jtEvent( self.vars.map1,position,row.lng,row.lat,mapradius,row);
		self.vars.dataCreat.id=row.id;
		$("#gzDialogbj .primray").click(function(){
				var tablesave={};
				tablesave.id=row.id;
				$.each(dataCount,function(i,dom){
					if(dom=="name"){
						tablesave[dom]=$("#gzDialogbj .tantk_right .tantk_Input").eq(0).find("input").val()
					}else if(dom=="lat"|| dom=="lng"){
						tablesave.lng=$("#gzDialogbj .tantk_right .tantk_Input").eq(1).find("input").val().split(",")[0];
						tablesave.lat=$("#gzDialogbj .tantk_right .tantk_Input").eq(1).find("input").val().split(",")[1];
					}else{
						tablesave[dom]=$("#gzDialogbj .tantk_right .tantk_Input").eq(i-1).find("input").val()
					}
				});
				console.log(tablesave)
				if(tablesave.id) {
					tablesave.id=row.id;
					$.each(dataCount,function(i,dom){
						self.vars.dataCreat[dom]=tablesave[dom]
					});
					self.addSubmit();
					return false
				}
			});
	},
	addSubmit:function(){
		let self = this;
		console.log(self.vars.dataCreat)
		if (self.vars.dataCreat.id){
			jrequest.put("/api/emergency",self.vars.dataCreat).then(res => {
				if(res) {
					$("#gzDialogbj").hide();
					self.ajaxtable()
				}
			})
		}

	},
	delate:function(row){
		var self=this;
		// Jdialog.confirm({ message: "是否删除?" }).on(function (e) {
		// 	if (e) {
		// 		jrequest.delete(`/api/emergency/${row.id}`, {}).then(res => {
		// 			self.ajaxtable()
		// 		})
		// 	}
		// });
		JPop.dialogDel({msg:'确定删除?',operate:true}).on(success => {
            if (success) {
                jrequest.delete(`/api/emergency/${row.id}`, {}).then(res => {
					self.ajaxtable()
				})
            }
        })
	},
	creatTablebaogao:function(res){
		var self=this;
		var columnsOne = [
			{
				field: 'id',
				title: '序号',
				align: 'center',
				formatter: function (value, row, index) {
					var pageSize=self.vars.searchTabmove.size;
					var pageNumber=self.vars.searchTabmove.page;
					return pageSize * (pageNumber - 1) + index + 1;
				}
			},
			{
				field: 'name',
				title: '报告名称',
			},
			{
				field: 'emergencyMan',
				title: '事件发起人',
			},
			{
				field: 'emergencyName',
				title: '事件名称',
			},
			{
				field: 'createTime',
				title: '创建时间',
			}
		];
		self.tableEvent("check_detailbox",res.data,self.vars.searchTabmove,columnsOne,true,res.total);
	},
	ajaxbaogao:function(){
		var self=this;
		jrequest.get(urls.datssgbaogao,self.vars.searchTabmove).then(res => {
			console.log("123",res)
			self.vars.datatable = res.data;
			$('#check_detailbox').bootstrapTable('destroy');
			self.creatTablebaogao(res)
		})
	},
	/**
	 * 表格操作按钮
	 * */
	operateFormatterManage: function () {
		return [
			'<button id="table_edit" class="ran_xq-manage">修改</button>&nbsp&nbsp',
			'<button id="table_del" class="dele-row-manage">删除</button>&nbsp&nbsp'
		].join('');
	},
	btnEvent:function(){
		var self=this;
		$(".tab_contentRain .tab_tit").click(function(){
			var index=$(this).index();
			$(this).addClass("active").siblings(".tab_tit").removeClass("active");
			$(".rTable_ran").eq(index).addClass("rTable_block").siblings("").removeClass("rTable_block")
		})
		$("#addqueren").click(function(){
			$("#gzDialog").show();
			self.vars.map.clearMap();
			$("#gzDialog .tantk_right .tantk_Input").each(function(i){
				$(this).find("input").val("")
			})
			$("#gzDialog .jdialog-btn .j-btn").eq(0).click(function(){
				$("#gzDialog").hide();
			})
		});
		$(".close-div").click(function(){
			$("#gzDialog").hide();
			$("#gzDialogbj").hide();
			$("#gzDialogsc").hide();
		});
		$(".prinqx").click(function(){
			$("#gzDialog").hide();
			$("#gzDialogbj").hide();
			$("#gzDialogsc").hide();
		});
		$(".rain_top button").click(function(){
			var index=$(this).index();
			$(this).addClass("ran_hactve").siblings("").removeClass("ran_hactve");
		})
		$(".rTable_ran").eq(0).find(".search-div .j-search-btn button").eq(0).off("click").click(function(){
			self.vars.searchTable.name=$(".rTable_ran").eq(0).find(".j-search-row .item").eq(0).find("input").val();
			var num=$("#selcet5").find("option:selected").index()-1;
			if(num==-1){
				self.vars.searchTable.status="";
			}else{
				self.vars.searchTable.status=num;
			};
			self.vars.searchTable.page=1;
			self.vars.searchTable.size=10;
			self.ajaxtable()
		});
		$(".rTable_ran").eq(0).find(".search-div .j-search-btn button").eq(1).off("click").click(function(){
			$(".rTable_ran").eq(0).find(".rtable_input").find("input").val("");
			$("#selcet5").siblings("input").val("全部");
			self.vars.searchTable.name="";
			self.vars.searchTabmove.page=1;
			self.vars.searchTabmove.size=10;
			self.ajaxtable()
		})
		$(".rTable_ran").eq(1).find(".search-div .j-search-btn button").eq(0).off("click").click(function(){
			self.vars.searchTabmove.emergencyName=$(".rTable_ran").eq(1).find(".rtable_input").eq(0).find("input").val();
			console.log("123",self.vars.searchTabmove)
			self.ajaxbaogao()
		});
		$(".rTable_ran").eq(1).find(".search-div .j-search-btn button").eq(1).off("click").click(function(){
			$(".rTable_ran").eq(1).find(".rtable_input").eq(0).find("input").val("");
			$(".rTable_ran").eq(1).find(".rtable_input").eq(1).find("input").val("");
			self.vars.searchTabmove.emergencyName="";
			self.vars.searchTabmove.startTime="";
			self.vars.searchTabmove.endTime="";
			self.vars.searchTabmove.page=1;
			self.vars.searchTabmove.size=10;
			self.ajaxbaogao()
		});
		$("#btn_add").click(function(){
			console.log('add');
			$("#addBook").modal()
		});
	},
	map:function(){
		var self=this;
		self.vars.map = new AMap.Map('map', {
			resizeEnable: true,
			center: [108.948024,34.263161],
			mapStyle:'amap://styles/darkblue',

		});
		self.vars.map1 = new AMap.Map('map1', {
			resizeEnable: true,
			center: [108.948024,34.263161],
			mapStyle:'amap://styles/darkblue',

		});
		var clickListener;
		var bind = function() {
			remove(); //防止重复绑定
			clickListener = AMap.event.addListener(self.vars.map, "click", function(e) {
				self.jtEvent(self.vars.map, e.lnglat,e.lnglat.getLng(),e.lnglat.getLat(),["8000","19000","30000"])
			});

		};
		//bt2的click的绑定事件
		var remove = function() {
			if (clickListener) {
				AMap.event.removeListener(clickListener);//移除事件，以绑定时返回的对象作为参数
			}
		};
		$(".bt1").click(function(){
			bind();
		});
		$(".bt2").click(function(){
			self.vars.map.clearMap();
			self.vars.map1.clearMap();
		})
		self.vars.map.on("click",function(e){
			if (clickListener){
				remove();
			}
		});
		self.vars.map1.on("click",function(e){
			if (clickListener){
				remove();
			}
		});
	},
	jtEvent:function(mapsx,position,positionlng,positionlat,radius,row){
		var self=this;
		var marker=new AMap.Marker({
			position: position,
			map: mapsx
		});
		var circle1,circle2,circle3;
		function creatRadius1(radius) {
			circle1 = new AMap.Circle({
				center: [positionlng, positionlat],
				radius: radius,
				strokeColor: "#f00",
				strokeWeight: 1,
				strokeOpacity: 0.3,
				fillOpacity: 0.5,
				strokeStyle: 'solid',
				strokeDasharray: [10, 10],
				fillColor: '#f00',
				zIndex: 100,
			});
		}
		function creatRadius2(radius) {
			circle2 = new AMap.Circle({
				center: [positionlng, positionlat],
				radius: radius,
				strokeColor: "#ff7f1b",
				strokeWeight: 1,
				strokeOpacity: 0.2,
				fillOpacity: 0.4,
				strokeStyle: 'solid',
				strokeDasharray: [10, 10],
				fillColor: '#ff7f1b',
				zIndex: 80,
			});
		}
		function creatRadius3(radius) {
			circle3=new AMap.Circle({
				center: [positionlng, positionlat],
				radius: radius,
				strokeColor: "#fce336",
				strokeWeight: 1,
				strokeOpacity: 0.1,
				fillOpacity: 0.1,
				strokeStyle: 'solid',
				strokeDasharray: [10, 10],
				fillColor: '#fce336',
				zIndex: 50,
			});
		}
		creatRadius1(radius[0]);
		creatRadius2(radius[1]);
		creatRadius3(radius[2]);
		mapsx.add([marker,circle1,circle2,circle3]);
		mapsx.setFitView([circle2]);
		$(".tantk_raduis1").val(radius[0]);
		$(".tantk_raduis2").val(radius[1]);
		$(".tantk_raduis3").val(radius[2]);
		$(".tantk_jw").val(positionlng + ',' + positionlat);
		var circleEditor1,circleEditor2,circleEditor3;
		circleEditor1 = new AMap.CircleEditor(mapsx, circle1);
		circleEditor2 = new AMap.CircleEditor(mapsx, circle2);
		circleEditor3 = new AMap.CircleEditor(mapsx, circle3);

		var m1,m2,m3;
		circleEditor1.on('adjust', function(e) {
			m1=e.target.getRadius();
		});
		circleEditor2.on('adjust', function(e) {
			m2=e.target.getRadius();
		});
		circleEditor3.on('adjust', function(e) {
			m3=e.target.getRadius();
		});
		function creatcircle1(circleEditor){
			circleEditor.on('end', function(e) {
				if(m1>=$(".tantk_raduis2").val()){
					$(".tantk_raduis1").val();
				}else{
					$(".tantk_raduis1").val(e.target.getRadius());
				}
			})
		};
		creatcircle1(circleEditor1);
		function creatcircle2(circleEditor){
			circleEditor.on('end', function(e) {
				if(m2>=$(".tantk_raduis3").val()){
					$(".tantk_raduis2").val();
				}else{
					$(".tantk_raduis2").val(e.target.getRadius());
				}
			})
		}
		creatcircle2(circleEditor2);
		function creatcircle3(circleEditor){
			circleEditor.on('end', function(e) {
				if(m3<=$(".tantk_raduis2").val()){
					$(".tantk_raduis3").val();
				}else{
					$(".tantk_raduis3").val(e.target.getRadius());
				}
			});
		}
		creatcircle3(circleEditor3)

		function creatid(id){
			$(id).find(".primray").unbind("click");
			$(id).find(".primray").click(function(){
				circleEditor1.close();
				circleEditor2.close();
				circleEditor3.close();
				if(m1>=$(".tantk_raduis2").val()){
					alert("不能大于第二个圈");
					mapsx.remove([circle1]);
					creatRadius1($(".tantk_raduis1").val());
					mapsx.add([circle1]);
					circleEditor1 = new AMap.CircleEditor(mapsx, circle1);
					creatcircle1(circleEditor1);
					creatOpen()
				}
				if(m2>=$(".tantk_raduis3").val()){
					alert("第二圈不能大于第三个圈");
					mapsx.remove([circle2]);
					creatRadius2($(".tantk_raduis2").val());
					mapsx.add([circle2]);
					circleEditor2 = new AMap.CircleEditor(mapsx, circle2);
					creatcircle2(circleEditor2);
					creatOpen()
				};
				if(m3<=$(".tantk_raduis3").val()){
					alert("第三圈不能小于第二圈");
					mapsx.remove([circle3]);
					creatRadius3($(".tantk_raduis3").val());
					mapsx.add([circle3]);
					circleEditor3 = new AMap.CircleEditor(mapsx, circle3);
					creatcircle3(circleEditor3);
					creatOpen()
				};
				var datcreat={}
				datcreat.name=$("#gzDialog .tantk_right .tantk_Input").eq(0).find("input").val();
				datcreat.lng=$("#gzDialog .tantk_right .tantk_Input").eq(1).find("input").val().split(",")[0];
				datcreat.lat=$("#gzDialog .tantk_right .tantk_Input").eq(1).find("input").val().split(",")[1];
				datcreat.riskRadius=$("#gzDialog .tantk_right .tantk_Input").eq(2).find("input").val();
				datcreat.riskText=$("#gzDialog .tantk_right .tantk_Input").eq(3).find("input").val();
				datcreat.warningRadius=$("#gzDialog .tantk_right .tantk_Input").eq(4).find("input").val();
				datcreat.warningText=$("#gzDialog .tantk_right .tantk_Input").eq(5).find("input").val();
				datcreat.vigilanceRadius=$("#gzDialog .tantk_right .tantk_Input").eq(6).find("input").val();
				datcreat.vigilanceText=$("#gzDialog .tantk_right .tantk_Input").eq(7).find("input").val();
				datcreat.emergencyDesc=$("#gzDialog .tantk_right .tantk_Input").eq(8).find("textarea").val();
				datcreat.tranType="1,2,3";
				var mapurl=HTTP+urls.datajjsj;
				if(id=="#gzDialog") {
					$.ajax({
						headers: {
							authorization: self.Authorization
						},
						dataType: "json",
						contentType: 'application/json',
						data: JSON.stringify(datcreat),
						url: mapurl,
						type: "post",
						success: function (data) {
							console.log(data);
							$("#check_detail").bootstrapTable('append', data);
							$("#gzDialog").hide()
						}
					})
				}

			});
		}
		creatid("#gzDialog");
		creatid("#gzDialogbj");

		function creatOpen(){
			circle1.on("click",function(e){
				circleEditor1.open();
			});
			circle2.on("click",function(e){
				circleEditor2.open();
			})
			circle3.on("click",function(e){
				circleEditor3.open();
				mapsx.setFitView([circle3]);
			});
		}
		creatOpen();

		$(".bt3").click(function(){
			circleEditor1.close();
			circleEditor2.close();
			circleEditor3.close();
			mapsx.setFitView([circle3]);
		})
	},
	selectedValue:function(){
		var self=this;
		var optionArr=["全部","待开启","已开启","已关闭"];
		self.creatId("#selcet5",optionArr);
	},
	creatId:function(id,optionArr){
		$(id).html();
		var str="";
		$.each(optionArr,function (i) {
			str+="<option value='"+optionArr[i]+"'>"+optionArr[i]+"</option>"
		});
		$(id).html(str);
		$(id).comboSelect();
	},

}

$(function(){
	common.initCatalogueMagePage();
});
